const path = require('path');

const express = require('express')
const multer = require('multer')

const app = express();

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, './uploads/');
    },
    filename: (req, file, callback) => {
        // callback(null, 'foo.png');
        // 修改文件名
        callback(null, Date.now() + path.extname(file.originalname));
    } 
}) 

const upload = multer({
    // dest: './uploads/'
    storage
});

app.use(upload.any());

app.post('/login', upload.any(), (req, res, next) => {
    console.log(req.body);
    res.end('用户登陆成功')
})


app.post('/upload', upload.single('file'), (req, res, next) => {
    console.log(req.files);
    res.end('file upload success')
})

// app.post('/upload', upload.array('file'), (req, res, next) => {
//     console.log(req.files);
//     res.end('file upload success')
// })

app.listen(8000, () => {
    console.log('form-data解析服务器启动成功');
})